package com.example.parkingproject.config;



import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.example.parkingproject.util.OrderUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Component
@Data
@Repository
public class aliyPay implements ApplicationRunner {
    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2021000122627290";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCObTAmUG5lKm3eWhEnxY0uB/GrnvdYoJ08PFegZqOU9qgl0dGmY8cFfflpt7cOVpomcNlag9RuL5tKu1hP4V4diENyRQ+imRA7+ltTu98MYmdlHVmsbzLpLTcfX5khqvWnZH2w49mZh1AnRmpZHhRwgIA49TLP//26VOv4RRRhE/gV4v79LBIR/YezxdjOlzdnYl/gXbw0lIi/7A5UAMo9rtQtq/Qcn3I9HHVXjssbr1huWfZsYTHuAjEYmAnYVz1pUuQbPzgGCvnHeyKdJRpE7a8at0AAyPHGiMrvD+VWOKjv1KLt+gJ1XkWUpQaLn48M64dLpLqo6u1y6N0Sl2AbAgMBAAECggEBAIMBCc3kI3boTQC9xZPYkSBq7C7Y3Fhn+9ihBFSktAhbzJRk5zruAe0aT2SfOEB3Bfkfyb0JD0d+gI9/vR5UhJYTdT5RKa18rsMMaB6f6uzgQoxuL8J80c5K6a3w59BhCO28cCRZoBgor2BA06c/FLvaZpZ9awyJQV+3pwjIuZMERLaNUzDZxW+doylHbi8I1na8dYJS9BO4+ZNJeGOzUYG0JsvWmHAHveGF9+aKZY/12yN4EoODVHBYhei2cKvMmz7pw5UtTKTKxl226xePZj8m3P2DcFyO0XWr6gGZo4FNZd+8I/HRdHsbR8kNx8uuXWQLZN5NVEDx4pjGF7mNljECgYEAxPlC/9eFuwjXMlKiKTqjKa7/Lw6/VVj5Ui04iINvHFGDkos5xmwQOvDRVrGkKLmYXUHA6uQIejRrDY6nyVJXT0vCtPuOoVfqYUiWztOMAqYvz84aIXhutm0Or2oa9jI2QnBjrYGlXVkdMdYtVXyQe28S4IV3yb0ZpUxKf5xu2lMCgYEAuRteJsb5/M+x3MIybb8lsBSj+n62ET+YsY3rUGKiS9A6BByNTBb3KRdnmvUm0s2r7qUT+Ou1hdBf39pJOYLQP0PTnuy5h2DvpYs/spG88cMQXG7nl17V0B/y0+hzqFSEOCkhMnMU0aGWSPElbHeXt2VlETmi147pAIWbjB9V+hkCgYAS4LTMIlK9xy4gs4y5GNaUYyIv12D9BAb5Gj5lzvryItnSTKef3bF8hKH7hhDumkMBBRFBMdOiKq3r4bUw9URK5D7n64gS7IMZCcTyU5ICmvzQFLeqZ4ls0Gzs4OVunQdYPL9A3MEFfknvAdDcdZC/U7jibcg6PHZdwj8NleeF4wKBgB+2jJ7gIBwsCTDlJHag9PgB0UYtR0wVNTfWNKaE/4E1n3rs2Vhb5uRnWhy2O3yKOBaeuZk/6M2QMZzMbRcpTjxIho0HyZFkDiqyfKs/RLu2y6rKZWsNmFwb3SJpBMemEzD7+vAFv19a68B7cAnxQ1DsUjBtS3TayxiHy2pqQ/ShAoGBALzhcW2dW0YKSVu63l+pGm6WMNmNpULsqAALy8SNTOPkC1huRp6LL4oIJhY/DDt8LXtWEAYT5FYb/CvRCPyV91bwN61qgTJVcBTAzJ9XDnD0QZzHC83J1n7pfXddHpMhYuZYe8NSN96Ucvc/Mp+lVar7uE4ZnvK/ELrLRPCslAQv";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjm0wJlBuZSpt3loRJ8WNLgfxq573WKCdPDxXoGajlPaoJdHRpmPHBX35abe3DlaaJnDZWoPUbi+bSrtYT+FeHYhDckUPopkQO/pbU7vfDGJnZR1ZrG8y6S03H1+ZIar1p2R9sOPZmYdQJ0ZqWR4UcICAOPUyz//9ulTr+EUUYRP4FeL+/SwSEf2Hs8XYzpc3Z2Jf4F28NJSIv+wOVADKPa7ULav0HJ9yPRx1V47LG69Ybln2bGEx7gIxGJgJ2Fc9aVLkGz84Bgr5x3sinSUaRO2vGrdAAMjxxojK7w/lVjio79Si7foCdV5FlKUGi5+PDOuHS6S6qOrtcujdEpdgGwIDAQAB";
    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "https://7i1102x083.goho.co/pay/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "https://7i1102x083.goho.co/back.html";

    // 签名方式
    public static String sign_type = "RSA2";

    // 字符编码格式
    public static String charset = "utf-8";

    // 支付宝网关
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    //订单超时时间
    public static String timeout = "5m";

    @Override
    public void run(ApplicationArguments args) throws Exception {

        log.debug("支付宝skd初始化完成");
    }

    public String pay(String name, String price) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);


        //组装订单信息，以JSON格式
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + OrderUtil.getOrderNo() + "\","
                + "\"total_amount\":\"" + price + "\","
                + "\"subject\":\"" + name + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
    /**
     * 验证签名
     */
    public boolean signVerification(Map<String, String[]> requestParams) {
        Map<String, String> params = new HashMap<>();
        for (String name : requestParams.keySet()) {
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        try {
            //调用SDK验证签名
            return AlipaySignature.rsaCheckV1(params, alipay_public_key, charset, sign_type);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return false;
        }
    }



    //pdhipg9135@sandbox.com
    //111111
}
